﻿Imports System.Text.RegularExpressions
Public Class clsNastro
  Private arrayNastro(0) As String
  Private ctrl As New ListBox
  Private _textBox As TextBox

  Public Sub New(ByVal controllo As ListBox)
    ctrl = controllo
  End Sub

  Public Sub Inserisci(
        ByVal display As String,
        ByVal operatore As String)
    If operatore.Equals("+") Or
        operatore.Equals("-") Or
        operatore.Equals("*") Or
        operatore.Equals("/") Then
      ReDim Preserve arrayNastro(
        arrayNastro.GetUpperBound(0) + 1)
      arrayNastro(arrayNastro.GetUpperBound(0)) =
        display & " " & operatore
      Call SvuotaNastro()
      Call AggiornaNastro()
    ElseIf operatore.Equals("=") Then
      ReDim Preserve arrayNastro(
        arrayNastro.GetUpperBound(0) + 1)
      arrayNastro(arrayNastro.GetUpperBound(0)) =
        "-------------------- ="
      ReDim Preserve arrayNastro(
        arrayNastro.GetUpperBound(0) + 1)
      arrayNastro(arrayNastro.GetUpperBound(0)) =
        display & " T"
      ReDim Preserve arrayNastro(
        arrayNastro.GetUpperBound(0) + 1)
      arrayNastro(arrayNastro.GetUpperBound(0)) =
        "======================"
    End If
  End Sub

  Public Sub SvuotaNastro()
    Do While ctrl.Items.Count() > 0
      ctrl.Items.RemoveAt(0)
    Loop
  End Sub

  Public Sub AggiornaNastro()
    If arrayNastro.GetUpperBound(0) = 0 Then
      ' non ci sono elementi
      ' da aggiungere al nastro
    Else
      For i As Integer = 1 To _
          arrayNastro.GetUpperBound(0)
        ctrl.Items.Add(arrayNastro(i))
      Next
    End If
    ctrl.SelectedIndex = ctrl.Items.Count - 1
  End Sub

  Public Sub AzzeraNastro()
    ReDim Preserve arrayNastro(0)
  End Sub

  Public Function componiNastro() As String
    Dim risposta As String = ""
    If arrayNastro.GetUpperBound(0) = 0 Then
      ' non ci sono elementi
      ' da aggiungere al nastro
    Else
      For i As Integer = 1 To _
          arrayNastro.GetUpperBound(0)
        risposta &= arrayNastro(i)
        If i < arrayNastro.GetUpperBound(0) Then
          risposta &= Environment.NewLine
        End If
      Next
    End If
    Return risposta
  End Function

  Public Sub SalvaNastro(
        ByVal nomeFile As String)
    Dim testo As String = componiNastro()
    If Not (testo.Equals("")) Then
      My.Computer.FileSystem.WriteAllText(
        nomeFile, testo, False)
    End If
  End Sub

  Public Sub RecuperaNastro(
        ByVal nomeFile As String)
    Dim testo As String =
      "" & Environment.NewLine &
      My.Computer.FileSystem.ReadAllText(nomeFile)
    Dim expreg As New Regex(
      Environment.NewLine)
    arrayNastro = Regex.Split(
      testo, Environment.NewLine)
    Call SvuotaNastro()
    Call AggiornaNastro()
  End Sub

  Public Sub StampaNastro()
    Calcolatrice.TestoDaStampare.Text =
      componiNastro()
  End Sub

End Class
